package student.aistar.service.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import student.aistar.mapper.WatchSeckillMapper;
import student.aistar.model.entity.Orders;
import student.aistar.model.entity.WebSeckill;
import student.aistar.service.IWatchSeckillService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class WatchSeckillServiceImpl implements IWatchSeckillService {

    @Autowired
    private WatchSeckillMapper watchSeckillMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<Map<String, Object>> beforeActive() {
        return watchSeckillMapper.beforeActive();
    }

    @Override
    public void updateStatus() {
        watchSeckillMapper.updateStatus();
    }

    @Override
    public List<Long> selectKillId() {
        return watchSeckillMapper.selectKillId();
    }

    @Override
    public void updateEndStatus() {
        watchSeckillMapper.updateEndStatus();
    }

    @Override
    public List<Long> selectEndKillid() {
        return watchSeckillMapper.selectEndKillid();
    }

    @Override
    public String getSatus(Integer killId) {
        String status = (String) redisTemplate.opsForValue().get("st_"+killId);
        if (status == null)
            status = "2";
        return status;
    }

    @Override
    public void save2Rbm(Integer userId, Integer killId) {
        Map<String,Integer> maps = new HashMap<>();

        maps.put("userId",userId);
        maps.put("killId",killId);

        rabbitTemplate.convertAndSend("xiaomi_wseckill",maps);
    }

    @Override
    public WebSeckill getSeckillTime(Integer seckillId) {
        return watchSeckillMapper.getStartTimeNow(seckillId);
    }

    @Override
    public int insertOrders(Orders order) {
        return watchSeckillMapper.insertOrders(order);
    }

    @Override
    public void insertOrderDetail(Integer orderId) {
        watchSeckillMapper.insertOrderDetail(orderId);
    }
}
